#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdlib.h>
#include<time.h>
int main() {
    DIR* dir;
    char buf[100];
    char arr[100];
    char res[100];
    char result[100];
    char old_path[50];
    char new_path[50];
    struct dirent* dent;
    dir = opendir("./");
    if (dir == NULL) {
        printf("open dir error\n");
        exit(-1);
    }
    dent = readdir(dir);
    while (dent != NULL) {
        if (dent->d_name[0] == '.') {
            dent = readdir(dir);
            continue;
        }
        else {
            memset(buf, 0, sizeof(buf));
            strcpy(buf, dent->d_name);
            printf("buf里面是：%s\n", buf);
            memset(arr, 0, sizeof(arr));
            arr[0] = buf[0];
            char result[100];

            char ch = '.';
            char* p = strrchr(buf, ch);
            if (p != NULL) {
                int i = 0;
                memset(res, 0, sizeof(res));  
                p++;  
                while (*p != '\0' && i < sizeof(res) - 1) {
                    res[i++] = *p++;
                }
                res[i] = '\0';  
            }
            printf("res最后三位是:%s\n", res);  
        
            if (p != NULL && strlen(buf) > 4)
            {
                arr[1] = buf[strlen(buf) - 1];
                arr[2] = buf[strlen(buf) - 2];
                printf("这是arr提取出来的:%s\n", arr);
                printf("\n");
                sprintf(result, "%s.%s", arr, res);

                printf("\n最终结果%s\n\n", result);

                snprintf(old_path, sizeof(old_path), "./%s", buf);
                snprintf(new_path, sizeof(new_path), "./%s", result);


                if (rename(old_path, new_path) == 0) {  
                    printf("文件 %s 成功重命名为 %s\n", old_path, new_path); 
                } else {  
                    perror("重命名失败");
                }
            }
            dent = readdir(dir);
        }

    }
}


